package com.ktjy.ktmall.controller;

import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.core.page.TableDataInfo;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.ktmall.domain.TOrder;
import com.ktjy.ktmall.domain.vo.Order;
import com.ktjy.ktmall.service.OrderService;
import com.ktjy.ktmall.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ktmall/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private TOrderService tOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }


    /**
     * 导出订单信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order) {
        try {
            List<Order> list = orderService.selectOrderList(order);
            ExcelUtil<Order> util = new ExcelUtil<>(Order.class);
            util.exportExcel(response, list, "订单信息");
        } catch (Exception e) {
            throw new RuntimeException("导出订单信息失败");
        }
    }


    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tOrderService.getById(id));
    }


    /**
     * 修改订单表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrder TOrder)
    {
        return toAjax(tOrderService.updateById(TOrder));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids) {
        return toAjax(orderService.removeByIds(ids));
    }
}

